package com.fengye.api.config;

import com.fengye.common.filter.CustomExceptionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author sunlei
 * @version 1.0
 * @date 2024/3/9 10:30:08
 * @description 自定义过滤器配置类
 */
@Configuration
public class MyFilterConfig {

    @Autowired
    CustomExceptionFilter customExceptionFilter;

    public FilterRegistrationBean<CustomExceptionFilter> registrationBean() {
        FilterRegistrationBean<CustomExceptionFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        // 给过滤器取名
        filterRegistrationBean.setName("CustomExceptionFilter");
        // 设置过滤器优先级，该值越小越优先被执行
        filterRegistrationBean.setOrder(1);
        // 注册自定义的filter
        filterRegistrationBean.setFilter(customExceptionFilter);
        // 设置拦截URL映射
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/**"));
        return filterRegistrationBean;
    }
}
